package confidence.tools;

import com.google.common.collect.Lists;
import joptsimple.internal.Strings;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    // 汉字数字的正则表达式
    private static final String regex = "[零一二三四五六七八九十百千万亿]+";
    private static final Pattern pattern = Pattern.compile(regex);

    /**
     * @param input
     * @return
     */
    public static String getChineseNumber(String input) {
        Matcher matcher = pattern.matcher(input);

        // 使用 StringBuilder 收集找到的汉字数字
        StringBuilder result = new StringBuilder();
        while (matcher.find()) {
            result.append(matcher.group());
        }

        // 将结果字符串切分成数组并返回
        return result.toString().trim();
    }

    /**
     * 获取两个字符串中的相同字符
     */
    public static char[] same_char(String text1, String text2){
        char[] arr1 = text1.toCharArray();
        char[] arr2 = text2.toCharArray();
        List<Character> list = new ArrayList<>();

        for(int i=0 ;i<arr1.length; i++) {
            for (int j=0; j<arr2.length; j++) {
                if(arr1[i] == arr2[j]) {
                    list.add(arr1[i]);
                    break;
                }
            }
        }
        return convertListToArray(list);
    }

    /**
     * 从字符串中抠出相同的部分
     * @return
     */
    public static char[] diff_char(String text, char[] remove) {
        if(StringUtils.isBlank(text)) {
            return new char[]{};
        }

        List<Character> result = new ArrayList<>();

        for(int i=0; i<text.length(); i++) {
            boolean flag = true;
            char ch = text.charAt(i);

            for (int j=0; j<remove.length; j++) {
                char chr = remove[j];
                if(ch == chr) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result.add(ch);
            }
        }
        return convertListToArray(result);
    }

    private static char[] convertListToArray(List<Character> list) {
        char[] result = new char[list.size()];
        for(int i=0; i<list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }

    public static String rtrim(String input, String target) {
        int index = input.lastIndexOf(target);

        while (index != -1) {
            input = input.substring(0, index);
            index = input.lastIndexOf(target);
        }

        return input;
    }

    public static String rstrip_all(String[] array, String target) {
        Arrays.sort(array, Comparator.comparingInt(String::length).reversed());
        for(int i=0; i<array.length; i++) {
            if(target.endsWith(array[i])) {
                target = CommonUtil.rtrim(target, array[i]);
            }
        }
        return target;
    }


    public static void main(String[] args) {
//        char[] remain = CommonUtil.diff_char("鼓楼校区", "鼓楼区".toCharArray());
//        System.out.println(remain.length);
//        for(char ch : remain) {
//            System.out.println(ch);
//        }

        System.out.println(CommonUtil.getChineseNumber("今天是三月二十六日，温度是一十二度，收入是五千元。"));
    }
}
